/* start GENERAL JS PROPERTIES */

    var audioAutoPlay           = false;
    var audioPlayerOpened       = true;
    var audioMuteOn             = false;
    var volumeValue             = 0.8;    
/* end GENERAL JS PROPERTIES */


	/*----------------- start footerListeners -----------------*/
    var playSign, pauseSign, volOnSign, volOffSign,
        isPlaying = false,
        initializePlayer = false,
        myPlaylist,
        firstPlay = true;
    function setupAudio(){        
        var audioPlayer = $("#audio-player");
        var audioItems = $("#audio-items");
        if( touchDevice == 1 ){
           audioPlayer.remove();
           return; 
        }
        if( audioItems.length <= 0 || audioItems.children().length <= 0 ){            
            if(audioPlayer.length > 0){
                audioPlayer.remove();
            }
            return;
        }
        /* setup data */
        audioAutoPlay = (audioPlayer.attr("data-auto-play") != undefined && audioPlayer.attr("data-auto-play") == "true" ) ? true : false;
        audioPlayerOpened = (audioPlayer.attr("data-player-opened") != undefined && audioPlayer.attr("data-player-opened") == "true" ) ? true : false;
        volumeValue = (audioPlayer.attr("data-player-volume") != undefined ) ? Number(audioPlayer.attr("data-player-volume")) : 0.5;
        tempVolume = volumeValue;
        audioPlayerInst = $("#audio-items");
        audioPlayer.css("width", $(".template-menu").width());
        var optionAudioBtn = $("#audio-options-button");
        var audioPlayerW = audioPlayer.width();
        var playerClosedVal = audioPlayerW - optionAudioBtn.width();
        var audioPlayerHolder = $("#audio-player-holder");  
        var isPlayerOpen = audioPlayerOpened; 
        var toolTip = $(".audio-player-tooltip-title","#audio-tooltip-holder"); 
        var prevTrack, playTrack, pauseTrack, nextTrack, 
            volMute,
            volBar,
            volScrubCurr,
            volScrubCurrW,
            volScrubMax,
            tooltipAudio;
            
        
        tooltipAudio = $( "#audio-player-tooltip" ,"#audio-player");
        prevTrack = $( "#audio-prev-track" ,"#audio-player-holder");
        playTrack = $( "#audio-play-pause-track" ,"#audio-player-holder");
        nextTrack = $( "#audio-next-track" ,"#audio-player-holder");
        volMute   = $( "#audio-volume-speaker" ,"#audio-player-holder");
        volBar    = $( "#audio-volume-bar" );
        volScrubCurr = $(".audio-volume-bar-scrub-current", volBar);
        volScrubMax = $(".audio-volume-bar-scrub-backg", volBar).width();
        volScrubCurr.css("width", volumeValue * volScrubMax);
        volScrubCurrW = volScrubCurr.width();
        
        if(audioPlayerOpened == true){
            audioPlayerHolder.css("left", "0px" );
            enableOver();
        } else{
            audioPlayerHolder.css("left", playerClosedVal );
            disableOver();
        }   
               
        optionAudioBtn.click(function(){
            if( isPlayerOpen == false ){
                isPlayerOpen = true;
                TweenMax.to( audioPlayerHolder, .4, {css:{left: "0px"}, ease: Sine.easeOut, onComplete:function(){enableOver();} });     
            } else{
                isPlayerOpen = false;
                disableOver();
                TweenMax.to( audioPlayerHolder, .4, {css:{left: playerClosedVal + "px"}, ease: Sine.easeOut });
            }
        });
        
        var audioArrSrc  = new Array();
        var audioArr     = new Array();
        var currSongID   = 0;
        var totalSongs   = 0;
        audioItems.find("#audio-track").each(
            function(){
                //audioArrSrc[ totalSongs ] = { src: $(this).attr("data-src"), title: $(this).attr("data-title") };
                audioArrSrc[ totalSongs ] = { 
                                                title: $(this).attr("data-title"),
                                                mp3:$(this).attr("data-src") + ".mp3",
                                                oga:$(this).attr("data-src") + ".ogg",
                                                wav:$(this).attr("data-src") + ".wav"
                                            };
                totalSongs++    
            }
        );
        var currTitle = "";
        function setTooltip( title ){
            if( currTitle == title ){
                return;
            }
            currTitle = title;
            var spanEl = $("span", toolTip);
            spanEl.css("width", "");
            spanEl.empty().append(title);
            if(spanEl.width() <= toolTip.width()){
               spanEl.css("width", "100%"); 
            }
        }
        audioItems.empty();        
        
        function initAudioPlayer(){
            myPlaylist = new jPlayerPlaylist(            
            {
                jPlayer: "#audio-items"
            }, 
            audioArrSrc, 
            {
                    playlistOptions: { 
                            loopOnPrevious: true,
                            enableRemoveControls: false
                    },
                    volume: volumeValue,
                    loop:true,
                    muted:false,    
                    swfPath: "js/audio-js/",
            		supplied: "oga, mp3, wav",
            		wmode: "window"
            });
           
            audioItems.bind($.jPlayer.event.play, function(event){
               setTooltip( event.jPlayer.status.media.title );
            });
        } 
        
        function changeAudioTemplate( id ){
            var newID = currSongID + id;
            if( newID > totalSongs - 1 ){ newID = 0; }               
            if( newID < 0 ){ newID = totalSongs - 1; }
                
            currSongID = newID;    
            myPlaylist.play(currSongID); 
        }       
        
        
        
        playSign = $(".audio-play-track-sign", playTrack),
        pauseSign = $(".audio-pause-track-sign", playTrack),
        volOnSign = $(".audio-volume-on-sign", volMute),
        volOffSign = $(".audio-volume-off-sign", volMute);
        
        if( audioAutoPlay == false ){
            playSign.css("opacity", "1");
            pauseSign.css("opacity", "0");
            setTooltip(audioArrSrc[currSongID].title);    
        } else{
            isPlaying = true;
            isAudioPlaying = true;
            if( initializePlayer == false ){
                initializePlayer = true;
                initAudioPlayer();
                firstPlay = false;
            }
            myPlaylist.play(currSongID);
            setTooltip(audioArrSrc[currSongID].title);
            playSign.css("opacity", "0");
            pauseSign.css("opacity", "1");  
        }
        
        if( audioMuteOn == false ){
            if( initializePlayer == true ){myPlaylist.option("muted", false);}
            volOnSign.css("opacity", "1");
            volOffSign.css("opacity", "0");    
        } else{
            if( initializePlayer == true ){myPlaylist.option("muted", true);}
            volOnSign.css("opacity", "0");
            volOffSign.css("opacity", "1");  
        }
        
        function enableOver(){
            addAudioControlsHover(prevTrack);
            addAudioControlsHover(playTrack);
            addAudioControlsHover(nextTrack);
            addAudioControlsHover(volMute);
        }
        function disableOver(){
            prevTrack.unbind('mouseenter mouseleave');
            playTrack.unbind('mouseenter mouseleave');
            nextTrack.unbind('mouseenter mouseleave');
            volMute.unbind('mouseenter mouseleave');
        }
        
        function addAudioControlsHover( elem ){
            var elemBack = $(".audio-controls-backg", elem);
            var elemBackCol = rgb2hex( elemBack.css("background-color") );            
            elem.hover(
                function(){
                    showAudioTooltip( true );
                    TweenMax.to( elemBack, .3, {css:{backgroundColor: themeColor}, ease: Sine.easeOut });    
                },
                function(){
                    showAudioTooltip( false );
                    TweenMax.to( elemBack, .3, {css:{backgroundColor: elemBackCol}, ease: Sine.easeOut });
                }
            );            
        }
        function showAudioTooltip( show ){
            if(tooltipAudio.length <= 0){return;}
            if( show == true ){
                tooltipAudio.css("display", "inline");
                TweenMax.killTweensOf(tooltipAudio);
                TweenMax.to( tooltipAudio, .4, {css:{opacity: "1"}, ease: Sine.easeOut });     
            } else {
                 TweenMax.to( tooltipAudio, .4, {css:{opacity: "0"}, ease: Sine.easeOut, onComplete: function(){
                    tooltipAudio.css("opacity", "0").css("display", "none");
                 } }); 
            }            
        }
        
        function checkInit(){
            if( initializePlayer == false ){
                initializePlayer = true;
                initAudioPlayer();
            }
        }
        prevTrack.click(function(){
            checkInit();
            isPlaying = true;
            isAudioPlaying = true;
            playSign.css("opacity", "0");
            pauseSign.css("opacity", "1"); 
            myPlaylist.previous();
        });
        nextTrack.click(function(){
            checkInit();
            isPlaying = true;
            isAudioPlaying = true;
            playSign.css("opacity", "0");
            pauseSign.css("opacity", "1"); 
            myPlaylist.next();
        });
        playTrack.click(function(){
            checkInit();
            if( isPlaying == true ){
                isPlaying = false;
                isAudioPlaying = false;
                myPlaylist.pause(); 
                playSign.css("opacity", "1");
                pauseSign.css("opacity", "0");
            } else{
                isPlaying = true;
                isAudioPlaying = true;
                if( firstPlay == false ){
                    
                    audioItems.jPlayer("option", "volume", tempVolume);
                    myPlaylist.play(); 
                } else {
                    firstPlay = true;
                    myPlaylist.play(currSongID); 
                }
                playSign.css("opacity", "0");
                pauseSign.css("opacity", "1");
            }
        });
        volBar.click(function(event){
            if( initializePlayer == false ){ return; }
            volScrubCurrW = event.pageX - $(this).offset().left;
            volScrubCurrW = Math.round((volScrubCurrW / volScrubMax) * 10) * 0.1;
            audioItems.jPlayer("option", "volume", volScrubCurrW);
            if( audioMuteOn == true && volScrubCurrW > 0 ){
               audioMuteOn = false;  
               audioItems.jPlayer("option", "muted", audioMuteOn);
               tempVolume = audioMuteOn;
               volOnSign.css("opacity", "1");
               volOffSign.css("opacity", "0");
            }
            volScrubCurrW =  volScrubMax * volScrubCurrW;
            if( volScrubCurrW > volScrubMax ){
                volScrubCurrW = volScrubMax;
            }
            TweenMax.to( volScrubCurr, .2, { css: {width: volScrubCurrW}, ease:Sine.easeOut });
        });
        volMute.click(function(){
            if( initializePlayer == false ){ return; }
            if( audioMuteOn == true ){
                audioMuteOn = false;     
                /*volScrubCurr.css("width", volScrubCurrW);*/   
                TweenMax.to( volScrubCurr, .3, { css: {width: volScrubCurrW}, ease:Sine.easeOut });        
                volOnSign.css("opacity", "1");
                volOffSign.css("opacity", "0");
            } else{
                audioMuteOn = true;
                TweenMax.to( volScrubCurr, .3, { css: {width: "0px"}, ease:Sine.easeOut });
                /*volScrubCurr.css("width", "0px");*/
                volOnSign.css("opacity", "0");
                volOffSign.css("opacity", "1");
            }
            audioItems.jPlayer("option", "muted", audioMuteOn);
        });
        
    }
    var audioPlayerInst;
    var isAudioPlaying = false;
    function audioPlayerPlay(){
        if( initializePlayer == false ){ return; }
        if(audioPlayerInst == undefined){return;}
        if( audioPlayerInst.length > 0 && isAudioPlaying == true && isPlaying == false){
            var currVol = tempVolume;
            audioPlayerInst.jPlayer("option", "volume", "0");             
            isPlaying = true;
            isAudioPlaying = true;
            playSign.css("opacity", "0");
            pauseSign.css("opacity", "1");
            
            var timeOutAudio = setTimeout(function(){
                    audioPlayerInst.jPlayer("play");
            }, 100);
            var intervalAudio = setInterval(function(){
                                audioPlayerInst.jPlayer("option", "volume", currVol);  
                                clearInterval(intervalAudio);                                
                                           
            }, 600);                       
        }
    }
    var tempVolume = 0;
    function audioPlayerPause(){
        if( initializePlayer == false ){ return; }
        if(audioPlayerInst == undefined){return;}
        if(audioPlayerInst.length > 0 ){
            tempVolume = audioPlayerInst.jPlayer("option", "volume");
            audioPlayerInst.jPlayer("option", "volume", "0");  
            isPlaying = false;
            playSign.css("opacity", "1");
            pauseSign.css("opacity", "0");
            var timeOutAudio = setTimeout(function(){
                /*audioPlayerInst.jPlayer("pause");*/
                if(isPlaying == true )myPlaylist.pause();        
            }, 100);
        }
    }
    function footerListeners(){
        $("#footer-social").find("#footer-social-holder").find("a").each(
                function(){
                  var aTitle = $("img", this).attr("title");  
                  $(this).hover(
                        function(){
                            $("#footer-social-tooltip").css("opacity", "0");
                            $("#footer-social-tooltip").css("display", "inline");
                            $("#footer-social-tooltip").css("visibility", "visible");
                            $("#footer-social-tooltip").empty();
                            $("#footer-social-tooltip").append('<span>' + aTitle + '</span>' );
                            TweenMax.to( $("#footer-social-tooltip"), .6, { css:{opacity:"1"}, ease:Circ.easeOut });
                        },
                        function(){
                            $("#footer-social-tooltip").css("opacity", "0");
                            $("#footer-social-tooltip").css("display", "none");
                            $("#footer-social-tooltip").css("visibility", "hidden");
                            $("#footer-social-tooltip").empty();
                    });  
                });
    }

	/*----------------- end footerListeners -------------------*/	